<?php
/*
 *
 *國慶活動管理
 */
namespace app\admin\controller;
use app\admin\model\NationalAward;
use think\Controler;
use app\admin\model\NationalAward as NationalAwardModel;
use app\admin\model\NationalUserLottery as NationalUserLotteryModel;
use  app\admin\model\User;
use think\Request;

class  National  extends  Index
{
    //1.1渲染奖品类别
    public function awardIndex()
    {
        return view();

    }
    //1.2加载奖品数据
    public function awardIndexData()
    {

    }

   //2.1渲染活动靓号
    public function niceIndex()
    {
        return view();
    }

    //2.2活动靓号详情
    public function niceShow()
    {
        return view();
    }

    //2.3活动靓号入库
    public function niceSet()
    {
        return view();
    }




    //3.1渲染中奖纪录
    public function recordIndex()
    {
        return view();
    }

    //4.1 渲染充值纪录
    public function rechargeIndex()
    {
        return view();
    }

    //5.1运营数据列表
    public function  lotteryIndex()
    {
        $nationalAwardModel =  new NationalAwardModel();
        $awards = $nationalAwardModel->field('award_id,award_name')->select();
        $this->assign('awards',$awards);
        return view();
    }


    //5.2运营数据请求
    public function lotteryIndexData()
    {
        $nationalUserLotteryModel =  new NationalUserLotteryModel();
        $page_config = array(
            'page' => input('offset',1),
            'list_rows' => input('limit',10)
        );
        $data = $nationalUserLotteryModel->getDataList($page_config);
       //halt($data);
        return json($data);
    }

    //
    public function lotteryEdit()
    {
        $id = input('id',0);
        $send_data = ['id'=> NULL,'user_id' =>'' ,'nickname'=> '',  'num' => '', 'award_name' => ''];

        if($id != 0){
            $nationalUserLotteryModel = new NationalUserLotteryModel();
            $condition = ['id' => $id];
            $user = $nationalUserLotteryModel->where($condition)->find();
            $rs = User::where(['id'=>$user['user_id']])->find();
            $send_data = ['id'=> $user['id'],'user_id' =>$rs['id'] ,'nickname'=> $rs['nickname'],  'num' => $user['num'], 'award_id' => $user['award_id'], 'award_name' => $user['award_name']];
        }

        //halt($send_data);
        $nationalAwardModel =  new NationalAwardModel();
        $awardData = $nationalAwardModel->select();
        $this->assign('award',$awardData);
        $this->assign('data',$send_data);
        return view();

    }
    //5.3运营数据添加
    public function lotteryAdd(Request $request)
    {
        $data = $request->post();
        if($data){
            $nationalUserLotteryModel =  new NationalUserLotteryModel();
            $rs = $nationalUserLotteryModel->save($data);
            if($rs){
                $this->success(lang('add') . lang('false'));
            }else{
                $this->error(lang('add') . lang('success'));
            }
        }
        $nationalAwardModel =  new NationalAwardModel();
        $awards = $nationalAwardModel->field('award_id,award_name')->select();
        $this->assign('awards',$awards);
        return view();
    }

    //5.3运营数据列表
    public function  lotteryShow($id = null)
    {
        if($id == null){
            $data = [
                'id' => null,
                'user_id' => '',
            ];
        }else{
       //     $lottery =
        }

        return view();
    }

    //5.3运营数据列表
    public function  lotterySet(Request $request)
    {

        $data = $request->post();
        $nationalUserLotteryModel =  new NationalUserLotteryModel();
        $nationalAwardModel =  new NationalAwardModel();
        $awards = $nationalAwardModel->where(['award_id' =>$data['award_id'] ])->field('award_name')->find();
        $data['award_name'] = $awards['award_name'];
      // halt($data);
        if($data['id']){
            //更新

            $rs = $nationalUserLotteryModel->save($data, ['id' => $data['id']]);
           // halt($rs);
            if($rs){
                $this->success(lang('edit') . lang('success'));
            }
             $this->error(lang('edit') . lang('false'));

        }else{
            //插入
            //unset($data['id']);
            $rs = $nationalUserLotteryModel->save($data);
            //halt($rs);
            if($rs){
                $this->success(lang('add') . lang('success'));
            }
            $this->error(lang('add') . lang('false'));

        }
    }

}